#!/usr/bin/env python

import sys
import os

def execute( f, command ):
    filename = f.name
    f.close()
    print >> sys.stderr, command
    os.system( command + " >> " + f.name )
    return open( filename, 'a' )

def printBoth( f, string, noNewline=False ):
    if noNewline:
        print string,
        print >> f, string,
    else:
        print string
        print >> f, string

def main():
    filename = "results.txt"
    executable = "/ufs2/home/users/d3x874/CptS_550/sowilo-xmt/bin/sowilo"
    f = open( filename, 'w' )
    runs = 3
    printBoth( f, "Running test with %i iterations a piece for statistical stability" % runs )
    #printBoth( f, "    Executing Serial test: 120 frames 10x10 pixels, 10 recursive objects" )
    #for r in range(runs):
        #printBoth( f, "      run %i: t="% r, noNewline=True )
        #f = execute( f, "mtarun %s 10 10 10 S N" % executable )
        #printBoth( f, "" )
    #printBoth( f, "    Executing Embarassingly Parallel test on 128 processors: 120 frames, 64x64 pixels, 20 recursive objects" )
    #for r in range(runs):
        #printBoth( f, "      run %i: t="% r, noNewline=True )
        #f = execute( f, "mtarun -m 128 -t 128 %s 64 64 20 P N" % executable )
        #printBoth( f, "" )
    #printBoth( f, "    Executing Future Parallel test on 128 processors: 120 frames, 64x64 pixels, 20 recursive objects" )
    #for r in range(runs):
        #printBoth( f, "      run %i: t="% r, noNewline=True )
        #f = execute( f, "mtarun -m 128 -t 128 %s 64 64 20 F N" % executable )
        #printBoth( f, "" )
    printBoth( f, "  Demonstrating load imbalance problem" )
    printBoth( f, "    Executing with 128 processors.  Rendering 120 frames 256x256 pixels" )
    for i in range( 2, 20, 4 ):
        for r in range(runs):
            printBoth( f, "      parallel run %i: o=%i t="% (r,i), noNewline=True )
            f = execute( f, "mtarun -m 128 -t 128 %s 256 256 %i P N" % ( executable, i ) )
            printBoth( f, "" )
            printBoth( f, "      future   run %i: o=%i t="% (r,i), noNewline=True )
            f = execute( f, "mtarun -m 128 -t 128 %s 256 256 %i F N" % ( executable, i ) )
            printBoth( f, "" )

    printBoth( f, "  Testing scalability: Scaling Problem Size, Fixed # of Processors" )
    printBoth( f, "    Executing with 128 prcessors.  Rendering 120 frames, 20 recursive objects" )
    #for i in range(7):
    w=512
    for r in range(runs):
        #w = 4 * 2**i
        printBoth( f, "      parallel run %i: w=%i, t=" % ( r, w ), noNewline=True )
        f = execute( f, "mtarun -m 128 -t 128 %s %i %i 20 P N" % ( executable, w, w ) )
        printBoth( f, "" )
        printBoth( f, "      future   run %i: w=%i, t=" % ( r, w ), noNewline=True )
        f = execute( f, "mtarun -m 128 -t 128 %s %i %i 20 F N" % ( executable, w, w ) )
        printBoth( f, "" )
    printBoth( f, "" )

if __name__ == "__main__":
    main()